# Concave
# Copyright (C) 2012 Daniel Malmqvist
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import sys
import constants
import g
from item import Item
import pygame

class Inventory():
    def __init__(self):
        self.items = [ [ 0 for i in range(5) ] for j in range(5) ]
        self.images = [ [ 0 for i in range(5) ] for j in range(5) ]
        
        self.headArmor = 0
        self.bodyArmor = Item(-1, -1, constants.TORSO(), 1)
        self.legsArmor = Item(-1, -1, constants.LEGS(), 1)
        self.beltArmor = 0
        self.feetArmor = Item(-1, -1, constants.FEET(), 1)
        self.weapon = Item(-1,-1, constants.DAGGER(), 0)
        
        
        self.bodyArmor.strExtra = 2
        self.bodyArmor.magExtra = 0
        self.bodyArmor.vitExtra = 0
        
        self.legsArmor.strExtra = 1
        self.legsArmor.magExtra = 0
        self.legsArmor.vitExtra = 0
        
        self.feetArmor.strExtra = 0
        self.feetArmor.magExtra = 0
        self.feetArmor.vitExtra = 2
        
        self.weapon.strExtra = 4
        self.weapon.magExtra = 0
        self.weapon.vitExtra = 0
        
        
        
    def addItem(self, item):
        # Look up next free slot and add the item
        #TODO: Potion stacks
        
        if (item.type == constants.BELT() or item.type == constants.FEET() or 
            item.type == constants.HEAD()  or item.type == constants.LEGS()  or 
            item.type == constants.TORSO() or item.type == constants.DAGGER() or 
            item.type == constants.LONGSPEAR() or item.type == constants.LONGSWORD() or 
            item.type == constants.RAPIER() or item.type == constants.SPEAR() or 
            item.type == constants.STAFF()):  
            for x in range(0,5):
                for y in range(0,5):
                    if self.items[y][x] == 0:
                        self.items[y][x] = item
                        self.images[y][x] = g.getItemImages()[(item.type, item.level)]
                        
                        return True
                
        return False
    

    def allItems(self):
        allItems = []
        for x in range(0,5):
            for y in range(0,5):
                allItems.append(self.items[y][x])
        
        allItems.append(self.beltArmor)
        allItems.append(self.bodyArmor)
        allItems.append(self.feetArmor)
        allItems.append(self.headArmor)
        allItems.append(self.legsArmor)
        allItems.append(self.weapon)
        
        return allItems

    
    def dropItem(self, x, y, dx = -1, dy = -1):
        # Drops an item on the ground
        if dx == -1 or dy == -1:
            i = self.items[y][x]
            
            if i != 0:
                i.x = g.getPlayer().getHitRect().left
                i.y = g.getPlayer().getHitRect().top
                
                i.hitBox = pygame.rect.Rect(i.x, i.y, 32, 32)
                g.addItem(i)
                
                self.items[y][x] = 0
                self.images[y][x] = 0
    
    def removeItem(self, x, y):
        # Removes an item from the inventory, when using an item etc.
        item = self.items[y][x]
        self.items[y][x] = 0
        self.images[y][x] = 0
        
        return item
        
    def printInv(self):
        for row in self.items:
            for e in row:
                if e == 0:
                    sys.stdout.write("0")
                else:
                    sys.stdout.write("#")
            sys.stdout.write("\n")
        sys.stdout.write("\n")
        
    def useItem(self,x,y):
        # Equips an item from the inventory
        item = self.items[y][x]
        
        if item == 0:
            return False
        
        if item.type == constants.BELT():
            if self.beltArmor == 0:
                self.beltArmor = item
                self.items[y][x] = 0
                self.images[y][x] = 0
                g.getPlayer().updateFromInventory()
                return True
            else:
                tmp = self.beltArmor
                self.beltArmor = item
                self.items[y][x] = tmp
                self.images[y][x] = g.getItemImages()[(self.items[y][x].type, self.items[y][x].level)]
                g.getPlayer().updateFromInventory()
                return True 
        elif item.type == constants.FEET():
            if self.feetArmor == 0:
                self.feetArmor = item
                self.items[y][x] = 0
                self.images[y][x] = 0
                g.getPlayer().updateFromInventory()
                
                return True
            else:
                tmp = self.feetArmor
                self.feetArmor = item
                self.items[y][x] = tmp
                self.images[y][x] = g.getItemImages()[(self.items[y][x].type, self.items[y][x].level)]
                g.getPlayer().updateFromInventory()
                return True
        elif item.type == constants.HEAD():
            if self.headArmor == 0:
                self.headArmor = item
                self.items[y][x] = 0
                self.images[y][x] = 0
                g.getPlayer().updateFromInventory()
                return True
            else:
                tmp = self.headArmor
                self.headArmor = item
                self.items[y][x] = tmp
                self.images[y][x] = g.getItemImages()[(self.items[y][x].type, self.items[y][x].level)]
                g.getPlayer().updateFromInventory()
                return True
        elif item.type == constants.LEGS():
            if self.legsArmor == 0:
                self.legsArmor = item
                self.items[y][x] = 0
                self.images[y][x] = 0
                g.getPlayer().updateFromInventory()
                return True
            else:
                tmp = self.legsArmor
                self.legsArmor = item
                self.items[y][x] = tmp
                self.images[y][x] = g.getItemImages()[(self.items[y][x].type, self.items[y][x].level)]
                g.getPlayer().updateFromInventory()
                return True
        elif item.type == constants.TORSO():
            if self.bodyArmor == 0:
                self.bodyArmor = item
                self.items[y][x] = 0
                self.images[y][x] = 0
                g.getPlayer().updateFromInventory()
                return True
            else:
                tmp = self.bodyArmor
                self.bodyArmor = item
                self.items[y][x] = tmp
                self.images[y][x] = g.getItemImages()[(self.items[y][x].type, self.items[y][x].level)]
                g.getPlayer().updateFromInventory()
                return True

        elif (item.type == constants.DAGGER() or 
                item.type == constants.LONGSPEAR() or item.type == constants.LONGSWORD() or 
                item.type == constants.RAPIER() or item.type == constants.SPEAR() or 
                item.type == constants.STAFF()):
            if self.weapon == 0:
                self.weapon = item
                self.items[y][x] = 0
                self.images[y][x] = 0
                g.getPlayer().type = item.type
                g.getPlayer().updateFromInventory()
                return True
            else:
                tmp = self.weapon
                self.weapon = item
                self.items[y][x] = tmp
                self.images[y][x] = g.getItemImages()[(self.items[y][x].type, 0)]
                g.getPlayer().type = item.type
                g.getPlayer().updateFromInventory()
                return True

            
        elif item.type == constants.HPPOTION() or item.type == constants.MPPOTION():
            # potions are not implemented yet.
            item.use()
            # If none left remove from inventory
            if item.stack <= 0:
                self.items[y][x] = 0
        else:
            return False
        
        pass

    def getImages(self):
        return self.images

    def toSaveText(self):
        # Returns a text blob representing all the items in the inventory
        text = ""
        if self.beltArmor != 0:
            text += self.beltArmor.toSaveText() + "\n"
        else:
            text += "0\n"
        
        if self.bodyArmor != 0:
            text += self.bodyArmor.toSaveText() + "\n"
        else:
            text += "0\n"
        
        if self.feetArmor != 0:
            text += self.feetArmor.toSaveText() + "\n"
        else:
            text += "0\n"
            
        if self.headArmor != 0:
            text += self.headArmor.toSaveText() + "\n"
        else:
            text += "0\n"
        
        if self.legsArmor != 0:
            text += self.legsArmor.toSaveText() + "\n"
        else:
            text += "0\n"
        
        if self.weapon != 0:
            text += self.weapon.toSaveText() + "\n"
        else:
            text += "0\n"
        
        for x in range(0,5):
            for y in range(0,5):
                if x == 4 and y == 4:
                    if self.items[y][x] == 0:
                        text += "0"
                    else:
                        text += self.items[y][x].toSaveText()
                else:
                    if self.items[y][x] == 0:
                        text += "0\n"
                    else:
                        text += self.items[y][x].toSaveText() + "\n"
        return text
"""        
apa = Inventory()
apa.addItem(1)
apa.addItem(1)
apa.addItem(1)
apa.addItem(1)

apa.printInv()
"""
